package net.jgrm.product.controller;

import net.jgrm.redisadvanced.lock.RedisLockService;
import net.jgrm.redisadvanced.service.RedisStringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author: houyong
 * @date: 2025年09月01日 15:44
 */
@RestController
public class RedisTestController {

//    @Autowired
//    private RedisTemplate redisTemplate;

    @Autowired
    private RedisStringService redisStringService;
    @Autowired
    private RedisLockService redisLockService;

    @RequestMapping("/redis/test")
    public Object testRedis(@RequestParam("key") String key,
                            @RequestParam("value") String value) {
        redisStringService.set(key, value);
        return redisStringService.get(key, String.class);
    }

//    @RequestMapping("/redis/lock")
//    public Object testRedisLock(@RequestParam("key") String key) {
//        try {
//            // 修改等待时间为30秒，这样第二个请求会等待最多30秒
//            System.out.println("tryLock-->time:->" + System.currentTimeMillis());
//            boolean lock = redisLockService.tryLock(key, 0, 0, TimeUnit.SECONDS);
//            System.out.println("lock: " + lock);
//            if (lock) {
//                Thread.sleep(10000); // 减少睡眠时间便于测试
//                System.out.println("Lock acquired");
//                redisLockService.unlock(key);
//                return "success";
//            } else {
//                System.out.println("Lock not acquired");
//                return "fail Lock not acquired";
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "error";
//        }
//
//    }
//
//
//    @RequestMapping("/redis/test/v2")
//    public Object testRedisLock() {
//        try {
//            // 修改等待时间为30秒，这样第二个请求会等待最多30秒
//            System.out.println("tryLock-->time:->" + System.currentTimeMillis());
//            Thread.sleep(10000); // 减少睡眠时间便于测试
//            return "success";
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "error";
//        }
//
//    }


    @GetMapping("/test-lock")
    public String testLock(@RequestParam String key) {
        try {
            // 尝试获取锁，等待0秒，持有10秒
            boolean acquired = redisLockService.tryLock(key, 0, TimeUnit.SECONDS);
            if (acquired) {
                try {
                    // 模拟业务处理
                    Thread.sleep(5000);
                    return "Lock acquired and processed successfully";
                } finally {
                    // 释放锁
                    redisLockService.unlock(key);
                }
            } else {
                return "Failed to acquire lock";
            }
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }


}
